package com.ssy.mall;

import java.util.concurrent.*;

public class CountDownDemo {
    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(8);
        ExecutorService threadPool = new ThreadPoolExecutor(
                2,
                5,
                2L,//多余的空闲线程的存活时间
                TimeUnit.SECONDS,
                //这个地方注意一定要设置范围 不然默认为最大值 会消耗资源
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        try {
            for (int i = 1; i <= 8; i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"\t离开教室");
                    countDownLatch.countDown();
                });
            }
            countDownLatch.await();
            System.out.println(Thread.currentThread().getName()+"\t班长关门走人");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }
}
